package ans.p43;

import test.Entrance;

/**
 * #      43
 * 名称   字符串相乘
 * 链接   https://leetcode-cn.com/problems/multiply-strings
 * 题解   [868](https://leetcode-cn.com/problems/multiply-strings/solution)
 * 通过率 44.7%
 * 难道   中等
 */

//fixme xxx
public class Solution {
    public void addToSb(StringBuilder sb, int index, int val) {
        System.out.println(sb.toString() + " " + index + " " + val);
        while (val != 0) {
            if (sb.length() > index) {
                val = val + sb.charAt(index) - '0';
                sb.setCharAt(index, (char) ('0' + (val % 10)));
            } else {
                sb.append((char) (val % 10 + '0'));
            }
            val /= 10;
            index++;
        }
    }

    @Entrance
    public String multiply(String num1, String num2) {
        int t = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = num1.length() - 1; i >= 0; i--) {
            for (int j = num2.length() - 1; j >= 0; j--) {
                int num = (num2.charAt(j) - '0') * (num1.charAt(i) - '0') + t;
                int val = num % 10;
                int index = (num2.length() - j) + (num1.length() - i) - 2;
                addToSb(sb, index, val);
                t = num / 10;
            }

        }
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("8949");
        new Solution().addToSb(stringBuilder, 2, 6);
        System.out.println(stringBuilder);
    }
}
